cd "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select"

use final.dta, clear
xi, pre(I_) i.nap40*i.an
save finalIndus.dta, replace

/* Residual Wages adjusted for industry by year fixed effects */
capture program drop wages
program define wages
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use finalIndus.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* I_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)

/* OLS */
ivreg2 drlsnd dimm Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv
end

wages, occp(all) 
wages, occp(cad) 
wages, occp(tecemp2) 
wages, occp(bc) 
wages, occp(bcnq) 

capture rm "C:\Users\Public\Documents\Tab21\TableA17.rtf"
/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\Public\Documents\Tab21\TableA17.rtf" /// 
, append title("bs")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)

/**********/ 
/* BARTIK */
/**********/
/*******************************/
/* Construct Bartik instrument */
/* initial composition of the commuting zone */
/* national growth of employment across industries */
/* local predicted employment growth */
use final.dta, clear
joinby cs2h using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\select\cs2h_cs1d.dta", unm(m)
tab _merge
save finalB.dta, replace

capture program drop bartik
program define bartik
version 11
syntax, nap40(name) 
/************************/
/* initial share per cz */
use finalB.dta, clear
keep if an==1976
gen zepop76=1
/* Ze pop in 1976 */
collapse (sum) zepop76 , by(ze )
save zepop76.dta, replace
/* Ze x nap pop per cz */
use finalB.dta, clear
keep if an==1976
gen nap=1
collapse (sum) nap , by(ze `nap40')
joinby ze using zepop76.dta, unm(m)
rm zepop76.dta
gen sh76=nap/zepop76
keep sh76 `nap40' ze
save sh76.dta, replace

/* aggregate evolution of `nap40' per year */
use finalB.dta, clear
keep if (btime==1 | (btime==2 & an==2007))
gen nnap=1
collapse (sum) nnap , by(`nap40' an)
/* growth in the number of workers per industries */
egen time=group(an)
egen inap=group(`nap40')
xtset inap time
gen gnap=ln(nnap)-ln(L.nnap)
keep an `nap40' gnap
save gnap.dta, replace

/* final bartik table */
use finalB.dta, clear
gen nobs=1
collapse (sum) nobs, by(ze an `nap40')
joinby `nap40' ze using sh76.dta, unm(m)
drop _merge
replace sh76=0 if missing(sh76)
joinby an `nap40' using gnap.dta, unm(m)
gen bartik=(sh76*gnap)
collapse (sum) bartik , by(ze an)
rename bartik b`nap40'
save bartik_`nap40'.dta, replace
end
bartik , nap40(nap40)
bartik , nap40(cs1d)

/* Residual Wages adjusted for industry by year fixed effects */
capture program drop wages
program define wages
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use final.dta, clear

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in 1982 1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
drop nbze

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 Y_*, by(ze an)
joinby ze an using bartik_nap40.dta, unm(m)
drop _merge
joinby ze an using bartik_cs1d.dta, unm(m)

/* OLS */
ivreg2 drlsnd dimm bnap40 Y_*  , partial(Y_* ) cl(ze)
estimates store bs`occp'ols

/* IV */
ivreg2 drlsnd (dimm  = dimm75 ) bnap40 Y_* , partial(Y_* ) cl(ze)
estimates store bs`occp'iv

end

wages, occp(all) 
wages, occp(cad) 
wages, occp(tecemp2) 
wages, occp(bc) 
wages, occp(bcnq) 

/* IV Balanced */
esttab bsalliv  bscadiv bstecemp2iv bsbciv bsbcnqiv ///
using "C:\Users\Public\Documents\Tab21\TableA17.rtf" /// 
, append title("Bartik nap 40")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
